﻿using System;
using System.Collections.Generic;
using System.Text;
using PostSharp.Aspects.Dependencies;

namespace Arcana.Contract.Method.Returns.Is.Not
{
    /// <summary>
    /// Represents a contract that adheres to <b>Method returns a value that is not in the specified collection.</b>
    /// </summary>
    [Serializable]
    [ProvideAspectRole(Roles.Contract)]
    [ProvideAspectRole(Roles.Method)]
    [ProvideAspectRole(Roles.ReturnValue)]
    [ProvideAspectRole(Roles.Conditional)]
    [AspectRoleDependency(AspectDependencyAction.Commute, Roles.Conditional)]
    public sealed class InAttribute : ComparingContractBase
    {
        private object[] _collection;

        /// <summary>
        /// Initializes a new instance of <b>Method.Returns.Is.Not.InAttribute</b>
        /// </summary>
        /// <param name="collection">The collection of objects which the method return value should not be inside.</param>
        /// <exception cref="System.ArgumentNullException"><i>collection</i> is <b>null</b>.</exception>
        public InAttribute(object[] collection)
        {
            if (collection == null) throw new ArgumentNullException();
            _collection = collection;
        }

        /// <summary>
        /// Performs validation on the retunr value.
        /// </summary>
        /// <param name="value">The return value that should be validated.</param>
        /// <returns><b>true</b> if <i>value</i> is validated, otherwise <b>false</b>.</returns>
        protected override bool ValidateValue(object value)
        {
            foreach (var item in _collection)
            {
                if (Comparer.Compare(value, item) == 0) return false;
            }
            return true;
        }
    }
}
